package cn.zjnu.study.controller;

import cn.zjnu.study.bean.User;
import cn.zjnu.study.bean.Video;
import cn.zjnu.study.bean.VideoX;
import cn.zjnu.study.service.UserService;
import cn.zjnu.study.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class MainController {

    @Autowired
    private UserService userService;

    @Autowired
    private VideoService videoService;

    @RequestMapping({"/", "/index"})
    public String index(Map<String, Object> map) {
        return "index";
    }

    @RequestMapping("/login")
    public String login(HttpSession session) {
        if (session.getAttribute("user") != null) {
            return "redirect:/";
        }
        return "login";
    }

    @RequestMapping("/register")
    public String register(HttpSession session) {
        if (session.getAttribute("user") != null) {
            return "redirect:/";
        }
        return "register";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.setAttribute("user", null);
        return "redirect:/login";
    }

    @RequestMapping("/account")
    public String info(HttpSession session) {
        if (session.getAttribute("user") == null) {
            return "redirect:/login";
        }
        return "account";
    }

    @RequestMapping("/uid/{uid}")
    public String userPage(Map<String, Object> map,
            HttpSession session,
            @PathVariable("uid") int userId) {
        User currentUser = (User) session.getAttribute("user");
        if (currentUser != null && currentUser.getUserId() == userId) {
            return "redirect:/account";
        }
        User user = userService.getUserById(userId);
        user.setPassword(null);
        map.put("user", user);
        return "user";
    }

    @RequestMapping("/vid/{vid}")
    public String videoPage(Map<String, Object> map,
                            @PathVariable("vid") int videoId) {
        Video video = videoService.getVideoById(videoId);
        User user = userService.getUserById(video.getUserId());
        VideoX videoX = new VideoX(user, video);
        map.put("video", videoX);
        return "video";
    }

    @RequestMapping("/upload")
    public String uploadPage(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        return "upload";
    }

    @RequestMapping("/test")
    public String test() {
        return "test";
    }

    @RequestMapping("/search/{videoName}")
    public String search(@PathVariable String videoName,
                         Model model) {
        List<Video> videos = videoService.searchByName(videoName);
        List<VideoX> videoXES = new ArrayList<>();
        for (Video video : videos) {
            User user = userService.getUserById(video.getUserId());
            VideoX videoX = new VideoX(user, video);
            videoXES.add(videoX);
        }
        model.addAttribute("videos", videoXES);
        return "search";
    }

}
